package com.summer.shopping.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.socket.TextMessage;

import java.util.Objects;

public class WSUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static TextMessage toTextMessage(Object obj) {
        try {
            return new TextMessage(Objects.requireNonNull(toJson(obj)));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



}
